package hang.xipujewelWeb.service.account;

import hang.xipujewelWeb.entity.account.User;
import hang.xipujewelWeb.entity.app.Jewel;
import hang.xipujewelWeb.entity.app.Order;
import hang.xipujewelWeb.entity.app.Ring;
import hang.xipujewelWeb.entity.app.ShoppingCart;
import hang.xipujewelWeb.repository.jpa.ShoppingCartDao;
import hang.xipujewelWeb.service.common.JpaManager;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

@Component
@Transactional
public class ShoppingCartManager extends JpaManager<ShoppingCart, ShoppingCartDao> {

	@Autowired
	private ShoppingCartDao shoppingCartDao;

	@Autowired
	private JewelManager jewelManager;

	@Autowired
	private OrderManager orderManager;

	public ShoppingCart findByUesrAndJewel(Long userId, Long jewelId) {
		return shoppingCartDao.findByUesrAndJewel(userId, jewelId);
	}

	public List<ShoppingCart> findByUesrAndRing(Long userId, Long ringId) {
		return shoppingCartDao.findByUesrAndRing(userId, ringId);
	}

	public List<ShoppingCart> findByUesr(Long userId) {
		return shoppingCartDao.findByUesr(userId);
	}

	//	public void deleteByUserJewel(Long userId, Long jewelId) {
	//		shoppingCartDao.deleteByUserJewel(userId, jewelId);
	//	}
	//
	//	public void deleteByUserRing(Long userId, Long ringId) {
	//		shoppingCartDao.deleteByUserRing(userId, ringId);
	//	}

	public void delete(List<Long> num, Long userId) {
		String str = num.toString().substring(1, num.toString().length() - 1);
		Session session = (Session) entityManager.getDelegate();
		String sql = "delete from ShoppingCart where jewel.id in(" + str + ") and user.id=" + userId;
		session.createQuery(sql).executeUpdate();
	}

	//提交订单
	@Transactional(readOnly = false)
	public String commit(List<ShoppingCart> shoppingCarts, String orderNum, User user) {// 新建一个订单实体Order，把购物车提交的订单放入该实体中
		List<Jewel> jewels = Lists.newArrayList();
		List<Ring> rings = Lists.newArrayList();
		//		String[] hasBuyJewel = {};//保存已被下单段钻石
		List<String> hasBuyJewel = Lists.newArrayList();
		Order order = new Order();
		for (ShoppingCart shoppingCartsCart : shoppingCarts) {
			if (shoppingCartsCart.getJewel() != null && shoppingCartsCart.getJewel().getId() != 0) {
				Jewel jewel = shoppingCartsCart.getJewel();
				if (jewel.getIsBuy() == 0) {
					updateJewel(jewel);
					jewels.add(jewel);
				} else {
					hasBuyJewel.add(jewel.getNum());
				}
				/*updateJewel(jewel);
				jewels.add(jewel);*/
			} else if (shoppingCartsCart.getRing() != null && shoppingCartsCart.getRing().getId() != 0) {
				Ring ring = shoppingCartsCart.getRing();
				rings.add(ring);
			}
			shoppingCartsCart.setStatus(1);
			shoppingCartDao.save(shoppingCartsCart);
		}
		if (jewels != null && jewels.size() != 0) {
			order.setJewels(jewels);
		}
		if (rings != null && rings.size() != 0) {
			order.setRings(rings);
		}
		order.setOrderNum(orderNum);
		order.setSubmitTime(new Date());
		order.setUser(user);
		order.setRemark(user.getShopAddress().getRemark());
		order.setRecieveUser(user.getShopAddress().getName());
		order.setPhone(user.getShopAddress().getCellphone());
		order.setCell(user.getShopAddress().getCell());
		order.setAddress(user.getShopAddress().getAddress());
		order.setEmail(user.getShopAddress().getEmail());
		order.setFax(user.getShopAddress().getFax());
		orderManager.save(order);
		if (hasBuyJewel != null && hasBuyJewel.size() != 0) {
			return hasBuyJewel.toString();
		} else {
			return "";
		}
	}

	private void updateJewel(Jewel jewel) {
		jewel.setIsBuy(1);
		jewelManager.save(jewel);
	}

	@Override
	protected ShoppingCartDao getDao() {
		// TODO Auto-generated method stub
		return shoppingCartDao;
	}

}
